import { defineStore } from 'pinia'

export const useOrderStore = defineStore('order', {
  // 状态
  state: () => ({
    // 订单列表
    orders: [],
    // 当前订单
    currentOrder: null,
    // 订单状态筛选
    statusFilter: 'all',
    // 加载状态
    loading: false,
    // 订单统计
    statistics: {
      unpaid: 0,
      unshipped: 0,
      shipped: 0,
      completed: 0,
      cancelled: 0
    }
  }),

  // 计算属性
  getters: {
    // 根据状态筛选订单
    filteredOrders: (state) => {
      if (state.statusFilter === 'all') {
        return state.orders
      }
      return state.orders.filter(order => order.status === state.statusFilter)
    },

    // 订单总数
    totalOrders: (state) => state.orders.length,

    // 订单总金额
    totalAmount: (state) => {
      return state.orders.reduce((total, order) => total + order.totalAmount, 0)
    },

    // 待支付订单
    unpaidOrders: (state) => {
      return state.orders.filter(order => order.status === 'unpaid')
    },

    // 待发货订单
    unshippedOrders: (state) => {
      return state.orders.filter(order => order.status === 'unshipped')
    }
  },

  // 操作方法
  actions: {
    // 创建订单
    async createOrder(orderData) {
      try {
        // TODO: 调用创建订单API
        const response = await createOrderApi(orderData)
        this.orders.unshift(response.data)
        this.updateStatistics()
        return response.data
      } catch (error) {
        console.error('创建订单失败：', error)
        throw error
      }
    },

    // 获取订单列表
    async fetchOrders() {
      this.loading = true
      try {
        // TODO: 调用获取订单列表API
        const response = await fetchOrdersApi()
        this.orders = response.data
        this.updateStatistics()
      } catch (error) {
        console.error('获取订单列表失败：', error)
      } finally {
        this.loading = false
      }
    },

    // 获取订单详情
    async fetchOrderDetail(orderId) {
      try {
        // TODO: 调用获取订单详情API
        const response = await fetchOrderDetailApi(orderId)
        this.currentOrder = response.data
        return response.data
      } catch (error) {
        console.error('获取订单详情失败：', error)
        throw error
      }
    },

    // 取消订单
    async cancelOrder(orderId) {
      try {
        // TODO: 调用取消订单API
        await cancelOrderApi(orderId)
        const order = this.orders.find(o => o.id === orderId)
        if (order) {
          order.status = 'cancelled'
          this.updateStatistics()
        }
      } catch (error) {
        console.error('取消订单失败：', error)
        throw error
      }
    },

    // 支付订单
    async payOrder(orderId, paymentData) {
      try {
        // TODO: 调用支付订单API
        await payOrderApi(orderId, paymentData)
        const order = this.orders.find(o => o.id === orderId)
        if (order) {
          order.status = 'unshipped'
          order.payTime = new Date().toISOString()
          this.updateStatistics()
        }
      } catch (error) {
        console.error('支付订单失败：', error)
        throw error
      }
    },

    // 确认收货
    async confirmReceive(orderId) {
      try {
        // TODO: 调用确认收货API
        await confirmReceiveApi(orderId)
        const order = this.orders.find(o => o.id === orderId)
        if (order) {
          order.status = 'completed'
          order.completedTime = new Date().toISOString()
          this.updateStatistics()
        }
      } catch (error) {
        console.error('确认收货失败：', error)
        throw error
      }
    },

    // 更新订单统计
    updateStatistics() {
      this.statistics = {
        unpaid: this.orders.filter(o => o.status === 'unpaid').length,
        unshipped: this.orders.filter(o => o.status === 'unshipped').length,
        shipped: this.orders.filter(o => o.status === 'shipped').length,
        completed: this.orders.filter(o => o.status === 'completed').length,
        cancelled: this.orders.filter(o => o.status === 'cancelled').length
      }
    },

    // 设置状态筛选
    setStatusFilter(status) {
      this.statusFilter = status
    }
  },

  // 持久化配置
  persist: {
    enabled: true,
    strategies: [
      {
        key: 'order',
        storage: localStorage,
        paths: ['statusFilter'] // 只持久化筛选条件
      }
    ]
  }
})